package com.codename1.samples;
import static com.codename1.ui.CN.*;

import com.codename1.components.SpanLabel;
import com.codename1.ui.*;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;


import java.io.IOException;
import java.util.ArrayList;

import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
 * of building native mobile applications using Java.
 */
public class BrowserComponentBlocked3300 {
private Form current;
private Resources theme;
boolean onloadCalled=false;
BrowserComponent bc;
public void init(Object context) {
    // use two network threads instead of one
    updateNetworkThreadCount(2);

    theme = UIManager.initFirstTheme("/theme");

    // Enable Toolbar on all Forms by default
    Toolbar.setGlobalToolbar(true);

    // Pro only feature
    Log.bindCrashProtection(true);

    addNetworkErrorListener(err -> {
        // prevent the event from propagating
        err.consume();
        if(err.getError() != null) {
            Log.e(err.getError());
        }
        Log.sendLogAsync();
        Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
    });        
}

public void start() {
    if(current != null){
        current.show();
        return;
    }
   Form hi = new Form("Hi World", new BorderLayout());
   bc=new BrowserComponent();
    bc.addWebEventListener("onLoad", new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {

if (onloadCalled) {return;} else
{onloadCalled=true;

//openAlertDialog("Test Dialog after onload","Blocking");

}

        }});

    bc.setPage("<HTML><BODY "
            +"style=\"display:flex;flex-direction:column;\""
            +" >"+
            createLines()+
            "</BODY></HTML>","");

    Button button=new Button("Press this button after HTML is loaded to test");
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            openAlertDialog("User activated test Dialog","Blocking");
        }
    });
    hi.add(BorderLayout.NORTH,button);
    hi.add(BorderLayout.CENTER,bc);
    hi.show();
  
}


private String createLines()
{
    String result="";
    for (int i=0;i<100;i++)
        result=result+"<BR/><P>TRY TO SELECT THIS TEXT / TRY TO SCROLL - LINE "+i+"</P>";
    return result;
}
public void stop() {
    current = getCurrentForm();
    if(current instanceof Dialog) {
        ((Dialog)current).dispose();
        current = getCurrentForm();
    }
}

public void destroy() {
}


Command okCommand=new Command("ok");

private void openAlertDialog( String s1, String s2)
{
    Dialog alertDialog=new Dialog(s1);
    Button okButton=new Button(okCommand);
    alertDialog.setLayout(BoxLayout.y());
    Container c1=new Container();
    c1.setLayout(BoxLayout.x());
    alertDialog.add(new SpanLabel(s2, "DialogBody"));
    c1.add(okButton);
    alertDialog.add(c1);
    Command result=alertDialog.showDialog();

}
}


